window.onload=function(){
	$(".chargerecords").css("backgroundColor","#224247").siblings().css("backgroundColor","#365d62");
	//查询
	$(".queryBoxBtn").click(function(){
		var writeContent = $(".chargeQueryInput").val();
		if(writeContent=="%"){
			hint("非法输入，请正确输入编号！");
		}else{
			window.location.href='/admin/device/chargeRecords?condition='+writeContent;
		}
	});
	//充值
	$(".rechargeBtn").click(function(event){
		var reg =/^[0-9]*$/;
		$(".labelBox").html("");
		event.stopPropagation();
		var thisId=$(this);
		thisId.css("visibility","hidden").siblings(".modeBox").show();
		//获取当前设备编号前两位字符串
		var deviceCodeHead=thisId.parent().siblings(".DeviceCodeCell").html();
		var CodeHead=deviceCodeHead.substring(0,2);
		//计量模式
		thisId.siblings(".modeBox").children(".meterageMode").unbind("click").click(function(event){
			event.stopPropagation();
			$(".labelBox").html("");
			layui.use('layer', function(){
			  	var layer = layui.layer;
			  	layer.open({
					type: 1,
					title: ['充值', 'font-size:18px;'],
					area: ['800px', '500px'],
				  	content: $(".meterageM"),
				}); 
			});
			//获取用户信息
			$(".chargeDeviceCode").html(thisId.parent().siblings(".DeviceCodeCell").html());
			$(".chargeGetUserName").html(thisId.parent().siblings(".userNameCell").html());
			$(".chargeResidueFlux").html(thisId.parent().siblings(".FluxCell").html());
			$(".chargeResidueDays").html(thisId.parent().siblings(".DaysCell").html());
			$.ajax({
				url:"/admin/api/device/install-address",
				type:"post",
				dataType:"json",
				data:{
					device_code:thisId.parent().siblings(".DeviceCodeCell").html()
				},
				success:function(data){
					if(data.status.errCode==0){
						$(".chargeGetAddress").html(data.data.install_address);
					}
				}
			})
			$(".charggeRecord_AddText").val($(this).parent().siblings(".chargeAddTextCell").html());
			DropinputFn(".chargeFluxBox",".moneyNumDrop",".fluxNumBox");//充值数值下拉框
			//取消充值
			$(".chargeNoBtn").unbind('click').click(function(){
				window.location.reload();
			})
			//确定充值
			var oldFluxCell=parseInt(thisId.parent().siblings(".FluxCell").html());
			$(".chargeOkBtn").unbind('click').click(function(e){

				if(oldFluxCell+parseInt($(".meterageM .fluxNumBox").html())>65000){
					$(".meterageM .labelBox").html("单次计量续费数值过大，请重新输入！");
					$(".meterageM .chargeFluxBox").val("");
					$(".meterageM .fluxNumBox").html("");
				}else{
					if(CodeHead=="20"){
						if(!reg.test($(".meterageM .chargeFluxBox").val())||parseInt($(".meterageM .chargeFluxBox").val())==0){
							$(".labelBox").html("充值金额请输入正整数");
							e.preventDefault();
						}else{
							$.ajax({
								url:"/admin/api/device/recharge",
								type:"post",
								dataType:"json",
								data:{
									device_id:thisId.siblings(".device_id").val(),
									money:parseInt($(".meterageM .payBox input").val()),
									flow:Math.round($(".meterageM .chargeFluxBox").val()/$(".meterageM .fluxPrice").val()),
									remark:$(".meterageM .charggeRecord_AddText").val()
								},
								success:function(data){
									if(data.status.errCode==0){
										thisId.parent().siblings(".revokeAddTextCell").html($(".meterageM .revokeAddText").val());
										//清空
										$(".meterageM .chargeFluxBox").val("");
										$(".meterageM .fluxNumBox").html("");
										$(".meterageM .labelBox").html("操作成功！"+data.status.message);
									}else{
										$(".meterageM .labelBox").html(data.status.message);
									}
								}
							})
						}
					}else if(CodeHead=="21"){
						if(!reg.test($(".meterageM .chargeFluxBox").val())||parseInt($(".meterageM .chargeFluxBox").val())==0){
							$(".meterageM .labelBox").html("充值金额请输入正整数");
							e.preventDefault();
						}else{
							$.ajax({
								url:"/admin/api/device/recharge/mode",
								type:"post",
								dataType:"json",
								data:{
									device_id:thisId.siblings(".device_id").val(),
									money:parseInt($(".meterageM .payBox input").val()),
									quantity:Math.round($(".meterageM .chargeFluxBox").val()/$(".meterageM .fluxPrice").val()),
									remark:$(".meterageM .charggeRecord_AddText").val(),
									mode:2
								},
								success:function(data){
									if(data.status.errCode==0){
										thisId.parent().siblings(".revokeAddTextCell").html($(".meterageM .revokeAddText").val());
										//清空
										$(".meterageM .chargeFluxBox").val("");
										$(".meterageM .fluxNumBox").html("");
										$(".labelBox").html("操作成功！"+data.status.message);
									}else{
										$(".labelBox").html(data.status.message);
									}
								}
							})
						}
					}else{
						hint("设备码非法！");
					}
				}
			})
			$("body").unbind("click").click(function(){
				thisId.css("visibility","visible").siblings(".modeBox").hide();
			})
		})
		
		//套餐模式
		thisId.siblings(".modeBox").children(".packageMode").unbind("click").click(function(event){
			$(".packageM .labelBox").html("");
			if(CodeHead=="20"){
				hint("该设备为第一批出厂设备，不具备套餐模式续费！");
			}else if(CodeHead=="21"){
				event.stopPropagation();
				//获取用户信息
				$(".chargeDeviceCode").html(thisId.parent().siblings(".DeviceCodeCell").html());
				$(".chargeGetUserName").html(thisId.parent().siblings(".userNameCell").html());
				$(".chargeResidueFlux").html(thisId.parent().siblings(".FluxCell").html());
				$(".chargeResidueDays").html(thisId.parent().siblings(".DaysCell").html());
				layui.use('layer', function(){
				  	var layer = layui.layer;
				  	layer.open({
						type: 1,
						title: ['充值', 'font-size:18px;'],
						area: ['800px', '500px'],
					  	content: $(".packageM")
					}); 
				});
				$.ajax({
					url:"/admin/api/device/install-address",
					type:"post",
					dataType:"json",
					data:{
						device_code:thisId.parent().siblings(".DeviceCodeCell").html()
					},
					success:function(data){
						if(data.status.errCode==0){
							$(".packageM .chargeGetAddress").html(data.data.install_address);
						}
					}
				})
				$("packageM .charggeRecord_AddText").val(thisId.parent().siblings(".chargeAddTextCell").html());
				DropinputFn(".chargeFluxBox",".moneyNumDrop",".fluxNumBox");//充值数值下拉框
				//取消充值
				$(".chargeNoBtn").unbind('click').click(function(){
					window.location.reload();
				})
				//确定充值
				var oldDaysCell=parseInt(thisId.parent().siblings(".DaysCell").html());
				$(".chargeOkBtn").unbind('click').click(function(e){
					if(oldDaysCell+parseInt($(".packageM .chargeFluxBox").html())>10000){
						$(".packageM .chargeFluxBox").val("");
						$(".packageM .labelBox").html("单次套餐续费数值过大，请重新输入！");
					}else{
						if(!reg.test($(".packageM .chargeFluxBox").val())||parseInt($(".packageM .chargeFluxBox").val())==0){
							$(".packageM .labelBox").html("续费天数请输入正整数");
							e.preventDefault();
						}else{
							$.ajax({
								url:"/admin/api/device/recharge/mode",
								type:"post",
								dataType:"json",
								data:{
									device_id:thisId.siblings(".device_id").val(),
									remark:$(".packageM .charggeRecord_AddText").val(),
									mode:1,
									quantity:parseInt($(".packageM .chargeFluxBox").val())
								},
								success:function(data){
									console.log(data.status);
									if(data.status.errCode==0){
										thisId.parent().siblings(".revokeAddTextCell").html($(".packageM .revokeAddText").val());
										//清空
										$(".packageM .chargeFluxBox").val("");
										$(".packageM .labelBox").html("操作成功！"+data.status.message);
									}else{
										$(".packageM .labelBox").html(data.status.message);
									}
								}
							})
						}
					}
				})
			}
		})
	})
}